﻿//***********************************************************************
// Assembly         : phpmk
// Author           : sy
// Created          : 06-14-2010
//
// Last Modified By : sy
// Last Modified On : 06-14-2010
// Description      : 
//
// Copyright        : (c) Another-D-Mention Software. All rights reserved.
//***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;
using System.Reflection;
using System.Windows.Forms;
using System.IO;
using phpmk.ADM.Out;

namespace phpmk.ADM.Utils
{
    internal static class Lang
    {
        private static string _Language = "en";
        private static bool loaded/* = false*/;
        private static Dictionary<String, String> language;

        public static string Language
        {
            set {
                _Language = value;
                LoadLanguage();
                if (!loaded) {
                    Output.GetInstance().Error("Cannot load language dictionary (" + _Language + ")");
                    throw (new IOException("Cannot load language dictionary"));
                }
            }
            get { return _Language; }
        }

        public static string GetString(String key) {
            if (language.ContainsKey(key)) {
                return language[key];
            } else {
                return key;
            }
        }

        private static void LoadLanguage() {
            language = new Dictionary<string, string>();
            String file = Path.GetDirectoryName(Application.ExecutablePath) + "\\Localization\\" + Language + ".ini";
            if (File.Exists(file)) {
                string[] lines = File.ReadAllLines(file, Encoding.UTF8);
                foreach (string line in lines) {
                    string[] pair = line.Split(new char[] { '=' });
                    if (pair.Length == 2)
                        language.Add(pair[0].Trim(), pair[1].Trim());
                }
                loaded = true;
            } else {
                loaded = false;
            }
        }
    }
}
